package com.caojm.mptools.commands;

import com.caojm.mptools.PermissionManager;
import com.caojm.mptools.utils.LoggerUtil;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;

public class BindCommand {

    private static final PermissionManager permissionManager = new PermissionManager();

    public static void register() {
    // 在命令注册回调中注册到调度器
    CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
        dispatcher.register(
            CommandManager.literal("mp")
                .then(CommandManager.literal("bind")
                    .then(CommandManager.argument("account", StringArgumentType.string())
                        .executes(context -> {
                            ServerCommandSource source = context.getSource();
                            String playerName = source.getName();

                            // 检查玩家是否有权限执行绑定操作
                            if (!permissionManager.hasPermission(playerName, "mingpixeltools.bind")) {
                            source.sendError(Text.of("你没有权限执行此操作！"));
                            LoggerUtil.logWarning("Player " + playerName + " attempted to use /mp bind without proper permission.");
                            return 0;
                            }

                            String account = StringArgumentType.getString(context, "account");

                            // 执行账号绑定的逻辑（假设绑定成功）
                            source.sendFeedback(() -> Text.of("账号绑定成功: " + account), false);
                            LoggerUtil.logInfo("Player " + playerName + " successfully bound their account to: " + account);
                            return 1;  // 返回成功
                        })
                    )
                )
        )
    );
    }
}
